package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.Chat;
import com.zcc.makingfriend.dao.PrivateChat;
import com.zcc.makingfriend.dao.User;
import com.zcc.makingfriend.dto.PrivateChatDto;
import com.zcc.makingfriend.mapper.ChatMapper;
import com.zcc.makingfriend.mapper.PrivateChatMapper;
import com.zcc.makingfriend.mapper.UserMapper;
import com.zcc.makingfriend.service.PrivateChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zcc
 */
@Service
public class PrivateChatServiceImpl implements PrivateChatService {

    @Autowired
    PrivateChatMapper privateChatMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ChatMapper chatMapper;

    @Override
    public int addPrivateChat(PrivateChat privateChat) {
        int i = privateChatMapper.addPrivateChat(privateChat);
        PrivateChat other = new PrivateChat(privateChat.getOtherId(), privateChat.getUserId());
        // 新增对方私聊记录
        int j = privateChatMapper.addPrivateChat(other);
        return i == 1 && j == 1 ? 1 : 0;
    }

    @Override
    public int deletePrivateChat(PrivateChat privateChat) {
        return privateChatMapper.deletePrivateChat(privateChat);
    }

    @Override
    public List<PrivateChatDto> getPrivateChat(Long userId) {
        List<PrivateChat> privateChat = privateChatMapper.getPrivateChat(userId);
        User myUser = userMapper.findUserById(userId);
        List<PrivateChatDto> privateChatDtos = new ArrayList<>();
        for (PrivateChat p: privateChat) {
            Long otherId = p.getOtherId();
            User user = userMapper.findUserById(otherId);
            PrivateChatDto privateChatDto = new PrivateChatDto();
            privateChatDto.setId(user.getId());
            privateChatDto.setUuid(user.getUuid());
            privateChatDto.setUsername(user.getUsername());
            privateChatDto.setAvatar(user.getAvatar());

            Chat chat = new Chat(myUser.getUuid(), user.getUuid());
            Long aLong = chatMapper.countChats(new Chat(myUser.getUuid(), user.getUuid()));
            // 统计未阅读数量
            privateChatDto.setUnReadCount(aLong);

            List<Chat> chats1 = chatMapper.findChats(new Chat(myUser.getUuid(), user.getUuid()));
            int size1 = chats1.size();
            List<Chat> chats2 = chatMapper.findChats(new Chat(user.getUuid(), myUser.getUuid()));
            int size2 = chats2.size();
            if (size1 == 0 && size2 == 0) {
                privateChatDto.setLastMessage(null);
                privateChatDto.setLastTime(null);
            } else if (size1 == 0){
                privateChatDto.setLastMessage(chats2.get(size2 -1).getMessage());
                privateChatDto.setLastTime(chats2.get(size2 -1).getTime());
            } else if (size2 == 0){
                privateChatDto.setLastMessage(chats1.get(size1 -1).getMessage());
                privateChatDto.setLastTime(chats1.get(size1 -1).getTime());
            } else {
                Chat chat1 = chats1.get(size1 - 1);
                Chat chat2 = chats2.get(size2 - 1);
                if (chat1.getTime().isBefore(chat2.getTime())) {
                    privateChatDto.setLastMessage(chats2.get(size2 -1).getMessage());
                    privateChatDto.setLastTime(chats2.get(size2 -1).getTime());
                } else {
                    privateChatDto.setLastMessage(chats1.get(size1 -1).getMessage());
                    privateChatDto.setLastTime(chats1.get(size1 -1).getTime());
                }
            }
            privateChatDtos.add(privateChatDto);
        }
        return privateChatDtos;
    }
}
